#include "mainuser.h"
#include "ui_mainuser.h"
#include "tools.h"
#include "mainwindow.h"

//用户界面，显示好友列表

MainUser::MainUser(User user, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainUser)
    , user(user)
{

    ui->setupUi(this);
    qDebug()<<"用户主界面："<<user.toQString();
    ui->textBrowser_username->setText(user.getUsername());
    socket = new QTcpSocket(this);
    socket->connectToHost("127.0.0.1",8090); //TCP连接，本机，8090端口
    if(socket->waitForConnected(2000)){//等待3s，连不上会返回fasle
        qDebug()<<"连接成功！";
    }else{
        qDebug()<<"连接失败！";
        qDebug()<<socket->errorString();
        return;
    }

    QStringList usernames;
    shuaxin(socket,table_friend,user.getUsername(),usernames);
    for (const QString &it : usernames) {
        ui->textBrowser_friends->append(it);
    }

    QStringList groupnames;
    shuaxin(socket,table_groupname,user.getUsername(),groupnames);
    for (const QString &it : groupnames) {
        ui->textBrowser_groupnames->append(it);
    }

}

MainUser::~MainUser()
{
    delete ui;
}

void MainUser::on_pushButton_shuaxin_friend_clicked()
{
    ui->textBrowser_friends->clear();
    QStringList usernames;
    shuaxin(socket,table_friend,user.getUsername(),usernames);
    for (const QString &it : usernames) {
        ui->textBrowser_friends->append(it);
    }
    ui->textBrowser_groupnames->clear();
    QStringList groupnames;
    shuaxin(socket,table_groupname,user.getUsername(),groupnames);
    for (const QString &it : groupnames) {
        ui->textBrowser_groupnames->append(it);
    }
}




void MainUser::on_pushButton_add_friend_clicked()
{
    QString friendname=ui->lineEdit_adddel_friendname->text();
    ui->lineEdit_adddel_friendname->clear();
    if(!usernameIsTrue(friendname)){
        QMessageBox::warning(this, "失败", "用户名格式错误！");
        return ;
    }
    User user1;
    user1.setUsername(friendname);
    //发送数据
    //发送请求头
    sendDatabase(socket,Database,Select,table_user);
    //发送数据
    sendUserData(socket,user1);

    //等待0.3秒
    waitForTimeout(1000);
    QByteArray data;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data = socket->readAll();
        qDebug()<<"添加好友--读取到数据";
    }else{
        qDebug()<<"添加好友--没有数据";
    }

    QDataStream in(data);
    in.setByteOrder(QDataStream::LittleEndian);

    int id;   //id
    QString username1; //用户名
    QString password1; //密码
    QString gender; //性别
    int age; //年龄
    QString phone; //电话号码
    QString state; //状态
    QString note; //个人说明

    in>>id>>username1>>password1>>gender>>age>>phone>>state>>note;
    //判断用户是否存在
    if(username1==""){
        QMessageBox::warning(this, "添加失败", "账户不存在！");
        return ;
    }
    //请求服务器
    sendDatabase(socket,Database,Insert,table_friend);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<user.getUsername()<<friendname;

    qDebug()<<user.getUsername()<<"请求添加"<<friendname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"添加好友--读取到数据";
    }else{
        qDebug()<<"添加好友--没有数据";
    }

    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);

}


void MainUser::on_pushButton_delete_friend_clicked()
{
    QString friendname=ui->lineEdit_adddel_friendname->text();
    ui->lineEdit_adddel_friendname->clear();
    if(!usernameIsTrue(friendname)){
        QMessageBox::warning(this, "失败", "用户名格式错误！");
        return ;
    }
    User user1;
    user1.setUsername(friendname);
    //发送数据
    //发送请求头
    sendDatabase(socket,Database,Select,table_user);
    //发送数据
    sendUserData(socket,user1);

    //等待1秒
    waitForTimeout(1000);
    QByteArray data;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data = socket->readAll();
        qDebug()<<"删除好友--读取到数据";
    }else{
        qDebug()<<"删除好友--没有数据";
    }

    QDataStream in(data);
    in.setByteOrder(QDataStream::LittleEndian);

    int id;   //id
    QString username1; //用户名
    QString password1; //密码
    QString gender; //性别
    int age; //年龄
    QString phone; //电话号码
    QString state; //状态
    QString note; //个人说明

    in>>id>>username1>>password1>>gender>>age>>phone>>state>>note;
    //判断用户是否存在
    if(username1==""){
        QMessageBox::warning(this, "删除失败", "账户不存在！");
        return ;
    }
    //请求服务器
    sendDatabase(socket,Database,Delete,table_friend);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<user.getUsername()<<friendname;

    qDebug()<<user.getUsername()<<"请求删除"<<friendname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"删除好友--读取到数据";
    }else{
        qDebug()<<"删除好友--没有数据";
    }
    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);
}


void MainUser::on_pushButton_add_groupname_clicked()
{
    QString groupname = ui->lineEdit_adddel_groupname->text();
    ui->lineEdit_adddel_groupname->clear();
    if(groupname==""){
        QMessageBox::warning(this,"失败", "分组名不能为空");
        return;
    }

    //请求服务器
    sendDatabase(socket,Database,Insert,table_groupname);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<user.getUsername()<<groupname;

    qDebug()<<user.getUsername()<<"请求增加分组"<<groupname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"增加分组--读取到数据";
    }else{
        qDebug()<<"增加分组--没有数据";
    }

    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);


}


void MainUser::on_pushButton_delete_groupname_clicked()
{
    QString groupname = ui->lineEdit_adddel_groupname->text();
    ui->lineEdit_adddel_groupname->clear();
    if(groupname==""){
        QMessageBox::warning(this,"失败", "分组名不能为空");
        return;
    }

    //请求服务器
    sendDatabase(socket,Database,Delete,table_groupname);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<user.getUsername()<<groupname;

    qDebug()<<user.getUsername()<<"请求增加分组"<<groupname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"添加好友--读取到数据";
    }else{
        qDebug()<<"添加好友--没有数据";
    }

    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);
}


void MainUser::on_pushButton_2_clicked()
{
    QString friendname = ui->lineEdit_chatname->text();

    if(friendname==""){
        QMessageBox::warning(this,"失败", "好友名不能为空");
        return;
    }

    QStringList exist_firends=ui->textBrowser_friends->toPlainText().split("\n");
    bool flag = true;
    for (const QString &it : exist_firends) {
        qDebug()<<it;
        if(it==friendname)
            flag=false;
    }
    if(flag){
        QMessageBox::warning(this,"失败", "好友不存在");
        return;
    }

    MainWindow *m = new MainWindow(user,socket,friendname,this);
    m->show();
}


void MainUser::on_pushButton_5_clicked()
{
    ui->textBrowser_2->clear();
    QString groupname= ui->lineEdit_chat_man_2->text();
    if(groupname==""){
        QMessageBox::warning(this,"失败", "分组名不能为空");
        return;
    }

    QStringList exist_groups=ui->textBrowser_groupnames->toPlainText().split("\n");
    bool flag = true;
    for (const QString &it : exist_groups) {
        qDebug()<<it;
        if(it==groupname)
            flag=false;
    }
    if(flag){
        QMessageBox::warning(this,"失败", "分组名不存在");
        return;
    }


    //请求服务器
    sendDatabase(socket,Database,Select,table_group);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<user.getUsername()<<groupname;

    qDebug()<<user.getUsername()<<"请求查询分组"<<groupname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"按分组查询--读取到数据";
    }else{
        qDebug()<<"按分组查询--没有数据";
    }
    QStringList usernames;
    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    in1>>usernames;
    for (const QString &it : usernames) {
        ui->textBrowser_2->append(it);
    }
}


void MainUser::on_pushButton_add_friTogro_clicked()
{
    QString username1=user.getUsername();
    QString groupname=ui->lineEdit_fri_gro_groupname->text();
    QString username2=ui->lineEdit_fri_gro_username->text();

    if(groupname==""||username2==""){
        QMessageBox::warning(this,"失败", "分组名和用户名不能为空");
        return;
    }

    //请求服务器
    sendDatabase(socket,Database,Insert,table_group);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<username1<<groupname<<username2;

    qDebug()<<user.getUsername()<<"请求移入分组"<<groupname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"移入分组--读取到数据";
    }else{
        qDebug()<<"移入分组--没有数据";
    }

    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);
}


void MainUser::on_pushButton_del_friTogro_clicked()
{
    QString username1=user.getUsername();
    QString groupname=ui->lineEdit_fri_gro_groupname->text();
    QString username2=ui->lineEdit_fri_gro_username->text();

    if(groupname==""||username2==""){
        QMessageBox::warning(this,"失败", "分组名和用户名不能为空");
        return;
    }

    //请求服务器
    sendDatabase(socket,Database,Delete,table_group);
    QByteArray data1;
    QDataStream out(&data1, QIODevice::WriteOnly);
    out.setByteOrder(QDataStream::LittleEndian);
    out<<username1<<groupname<<username2;

    qDebug()<<user.getUsername()<<"请求移出分组"<<groupname;
    socket->write(data1);

    //等待服务器
    //等待1秒
    waitForTimeout(1000);
    QByteArray data2;
    if (socket->bytesAvailable() > 0) {
        // 有数据可以读取
        data2 = socket->readAll();
        qDebug()<<"移出分组--读取到数据";
    }else{
        qDebug()<<"移出分组--没有数据";
    }

    QDataStream in1(data2);
    in1.setByteOrder(QDataStream::LittleEndian);
    QString res;
    in1>>res;
    QMessageBox::warning(this,"结果", res);
}

